﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EPiServer.Security;
using System.Web;
using EPiServer.Personalization;

namespace EPiServer.Research.VirtualRoles
{
    public class ScoreRole: VirtualRoleProviderBase
    {
        public string ScoreName { get; set; }
        public int Min { get; set; }
        public int Max { get; set; }

        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            base.Initialize(name, config);
            if (!string.IsNullOrEmpty(config["scorename"])) ScoreName = config["scorename"];
            if (!string.IsNullOrEmpty(config["min"]))
            {
                Min = int.Parse(config["min"]);
            }
            else Min = int.MinValue;
            if (!string.IsNullOrEmpty(config["max"]))
            {
                Max = int.Parse(config["max"]);
            }
            else Max = int.MaxValue;
        }

        public override bool IsInVirtualRole(System.Security.Principal.IPrincipal principal, object context)
        {
            if (HttpContext.Current == null) return false;
            if (EPiServerProfile.Current == null) return false;
            if (EPiServerProfile.Current[ScoreName] == null) return false;
            int v = (int)EPiServerProfile.Current[ScoreName];
            return (v > Min) && (v < Max);
            
        }
    }
}
